using ViScript.Core.Utils;

namespace ViScript.Core.Blocks
{
    public class QuotedBlock : NestedBlock
    {
        private readonly IEscapeStrategy escapeStrategy;
        private readonly char expectedQuotationMark;

        public QuotedBlock(IEscapeStrategy escapeStrategy, char expectedQuotationMark, NestedBlock parent)
            : base(parent)
        {
            this.escapeStrategy = escapeStrategy;
            this.expectedQuotationMark = expectedQuotationMark;
            AppendBlock(new RootBlock(escapeStrategy));
        }

        public override string Escape()
        {
            return expectedQuotationMark + escapeStrategy.Escape(base.Escape()) + expectedQuotationMark;
        }

        public override string Unescape()
        {
            return expectedQuotationMark + escapeStrategy.Unescape(base.Unescape()) + expectedQuotationMark;
        }

        public override void Append(char c)
        {
            if (c == expectedQuotationMark)
            {
                Parent.AppendBlock(new NormalBlock());
            }
            else
            {
                CurrentBlock.Append(c);
            }
        }

        public override bool ShouldAppendQuotedBlock(char c)
        {
            return false;
        }
    }
}